﻿using SmsLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Sms.Wins
{
    public partial class Form1 : Form
    {

        private ShortMessage sm;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PortTxt.Text = "10";
            RateTxt.Text = "57600";
        }

        private void ConnectBtn_Click(object sender, EventArgs e)
        {
            int port = Convert.ToInt32(PortTxt.Text);
            uint rate = Convert.ToUInt32(RateTxt.Text);
            sm = new ShortMessage(port, rate);
            var result = sm.Start();
            if (result)
            {
                StopBtn.Enabled = true;
                ConnectBtn.Enabled = false;
            }
            Statuslbl.Text = result ? "状态：连接成功" : "状态：连接失败";
        }

        private void StopBtn_Click(object sender, EventArgs e)
        {
            var result = sm.Stop();
            if (result)
            {
                StopBtn.Enabled = false;
                ConnectBtn.Enabled = true;
            }
            Statuslbl.Text = result ? "状态：断开成功" : "状态：断开失败";
        }

        private void SendBtn_Click(object sender, EventArgs e)
        {
            var result = sm.Send(PhoneNoTxt.Text, ShortMessageTxt.Text);
            Statuslbl.Text = result ? "状态：发送信息成功" : "状态：发送信息失败";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (sm == null) return;
            var msg = sm.Receive();
            if (msg != null)
            {
                var msgStr = msg.Message;
                var receTime = msg.ReceiveTime;
                string receMsg = "号码：" + msg.PhoneNumber + "\r\n";
                receMsg += "消息：" + msgStr + "\r\n";
                receMsg += "接收时间：" + receTime + "\r\n\r\n";
                ReceiveTxt.Text += receMsg;
            }
        }
    }
}
